/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.sys.entity.User;
import cn.net.ecode.modules.sys.service.UserService;
import cn.net.ecode.modules.sys.utils.UserUtils;

/**
 * 集团管理员Controller
 * @author ThinkGem
 * @version 2015-7-24
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/corpAdminUser")
public class CorpAdminUserController extends BaseController {

	@Autowired
	private UserService userService;

	@ModelAttribute
	public User get(String userCode, boolean isNewRecord) {
		return userService.get(userCode, isNewRecord);
	}
	
	@RequiresPermissions("sys:corpAdminUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(User user, Model model) {
		return "modules/sys/corpAdminUserList";
	}

	@RequiresPermissions("sys:corpAdminUser:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<User> listData(User user, HttpServletRequest request, HttpServletResponse response) {
		user.setUserType(User.USER_TYPE_ONLYLOGIN);	// 仅登录用户
		user.setMgrType(User.MGR_TYPE_CORP_ADMIN);	// 集团管理员
		Page<User> page = userService.findAllPage(new Page<User>(request, response), user);
		return page;
	}

	@RequiresPermissions("sys:corpAdminUser:view")
	@RequestMapping(value = "form")
	public String form(User user, HttpServletRequest request, Model model) {
		if (user.getIsNewRecord()){
			user.setCorpCode(""); // 归属集团Code
			user.setCorpName("");  // 归属集团Name
		}
		model.addAttribute("user", user);
		return "modules/sys/corpAdminUserForm";
	}

	@RequiresPermissions("sys:corpAdminUser:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(User user, HttpServletRequest request) {
		if (Global.isDemoMode()) {
			return renderResult(Global.FALSE, "演示模式，不允许操作！");
		}
		if (!user.getCurrentUser().isSuperAdmin()){
			return renderResult(Global.FALSE, "越权操作，只有超级管理员才能修改此数据！");
		}
		if (User.isAdmin(user.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此用户！");
		}
		StringBuilder message = new StringBuilder();
		if (!beanValidator(message, user)) {
			return renderResult(Global.FALSE, message);
		}
		user.setUserType(User.USER_TYPE_ONLYLOGIN); // 仅登录用户
		user.setMgrType(User.MGR_TYPE_CORP_ADMIN); // 集团管理员
		userService.save(user);
		// 如果修改的是当前用户，则清除当前用户缓存
		if (user.getUserCode().equals(UserUtils.getUser().getUserCode())) {
			UserUtils.clearCache();
		}
		return renderResult(Global.TRUE, "保存集团管理员'" + user.getUserCode() + "'成功");
	}

}
